package com.tools.generator.service.impl;

import cn.hutool.core.text.NamingCase;
import com.tools.common.utils.Asserts;
import com.tools.common.utils.DateUtils;
import com.tools.generator.config.GeneratorConfig;
import com.tools.generator.config.ToolDataSource;
import com.tools.generator.entity.TableEntity;
import com.tools.generator.entity.TableFieldEntity;
import com.tools.generator.entity.template.GeneratorInfo;
import com.tools.generator.mapper.TableMapper;
import com.tools.generator.service.DataSourceService;
import com.tools.generator.service.TableFieldService;
import com.tools.generator.service.TableService;
import com.tools.generator.utils.QueryUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * @author Admin
 */
@Slf4j
@Service
public class TableServiceImpl implements TableService {

    @Autowired
    private TableMapper tableMapper;

    @Autowired
    private TableFieldService tableFieldService;

    @Autowired
    private DataSourceService dataSourceService;

    @Autowired
    private GeneratorConfig generatorConfig;

    @Override
    public List<TableEntity> page(TableEntity query) {
        return tableMapper.selectGenTableList(query);
    }

    @Override
    public TableEntity getByTableName(String tableName) {
        TableEntity tableEntity = new TableEntity();
        tableEntity.setTableName(tableName);
        return tableMapper.selectGenTable(tableEntity);
    }

    @Override
    public List<TableEntity> selectTableByIds(List<Long> ids) {
        return tableMapper.selectTableByIds(ids);
    }

    @Override
    public void deleteBatchIds(Long[] ids) {
        // 删除表
        tableMapper.deleteGenTableByIds(Arrays.asList(ids));

        // 删除列
        tableFieldService.deleteBatchTableIds(ids);
    }

    @Override
    public void tableImport(Long datasourceId, String tableName) {
        ToolDataSource dataSource = dataSourceService.get(datasourceId);
        // 查询表是否存在
        TableEntity table = this.getByTableName(tableName);
        // 表存在
        Asserts.isNotNull(table,tableName + "已存在");

        // 从数据库获取表信息
        table = QueryUtils.getTable(dataSource, tableName);

        // 代码生成器信息
        GeneratorInfo generator = generatorConfig.getGeneratorConfig();

        // 保存表信息
        table.setPackageName(generator.getProject().getPackageName());
        table.setVersion(generator.getProject().getVersion());
        table.setBackendPath(generator.getProject().getBackendPath());
        table.setFrontendPath(generator.getProject().getFrontendPath());
        table.setAuthor(generator.getDeveloper().getAuthor());
        table.setEmail(generator.getDeveloper().getEmail());
//        table.setFormLayout(FormLayoutEnum.ONE.getValue());
//        table.setGeneratorType(GeneratorTypeEnum.ZIP_DOWNLOAD.ordinal());
        table.setClassName(NamingCase.toPascalCase(tableName));
        // todo 模块名待优化
        table.setModuleName(QueryUtils.getModuleName(table.getModuleName()));
        table.setFunctionName(QueryUtils.getFunctionName(tableName));
        table.setCreateTime(DateUtils.getNowDateTimeString());
        this.save(table);

        // 获取原生字段数据
        List<TableFieldEntity> tableFieldList = QueryUtils.getTableFieldList(dataSource, table.getId(), table.getTableName());
        // 初始化字段数据
        tableFieldService.initFieldList(tableFieldList);

        // 保存列数据
        tableFieldList.forEach(tableFieldService::save);

        try {
            //释放数据源
            dataSource.getConnection().close();
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public void sync(Long id) {

    }

    @Override
    public void updateById(TableEntity table) {
        tableMapper.updateGenTable(table);
    }

    @Override
    public TableEntity getById(Long id) {
        return tableMapper.selectGenTableById(id);
    }

    @Override
    public void save(TableEntity table) {
        tableMapper.insertGenTable(table);
    }
}
